var _cardUtil = _interopRequireDefault(require("../../../utils/card-util")), _radarUtil = require("../../../utils/radar-util"), _contactUtil = require("../../../utils/contact-util");

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var e = 0, a = new Array(t.length); e < t.length; e++) a[e] = t[e];
        return a;
    }
}

var _global = global, api = _global.api, LIMIT = 40;

Page({
    data: {
        isOpen: !1,
        activeIndex: 0,
        useNum: 0,
        payNum: 0,
        userList: [],
        card: {}
    },
    onLoad: function(t) {
        this.query = t, this.setData({
            card: this.$appData.card,
            guid: t.guid || !1,
            isOpen: "true" === t.isopen
        });
    },
    onUnload: function() {
        this.isChange && this.handleOpen();
    },
    onPullDownRefresh: function() {
        1 == +this.data.activeIndex ? this.loadUserList(!0, wx.stopPullDownRefresh) : wx.stopPullDownRefresh();
    },
    onReachBottom: function() {
        if (1 == +this.data.activeIndex) {
            if (this.noMore) return;
            this.loadUserList();
        }
    },
    onNavChange: function(t) {
        var e = t.detail, a = e.index, i = e.reset;
        this.setData({
            activeIndex: a
        }), 1 === a && i && (this.loadUserList(!0), this.initRadarEvent());
    },
    initRadarEvent: function() {
        var r = this;
        this.$addListener("radar.user.del", function(e) {
            var t = r.data, a = t.userList, i = t.useNum;
            r.setData({
                useNum: i - e.length,
                userList: a.filter(function(t) {
                    return !e.includes(t.cardid);
                })
            });
        }), this.$addListener("radar.user.change", function() {
            return r.loadUserList(!0);
        });
    },
    loadUserList: function(r, t) {
        var n = this;
        r && (this.offset = 0);
        var e = this.data.card;
        api.minicard.card.getminicardgroupuserlist({
            data: {
                guid: e.guid,
                op: "use",
                limit: LIMIT,
                offset: this.offset
            },
            complete: t
        }).then(function(t) {
            var e = t.data.list;
            if (r) n.setData({
                userList: e,
                useNum: t.data.usenum,
                payNum: t.data.chargenum
            }); else {
                var a = (n.data.userList || []).length, i = {};
                e.forEach(function(t, e) {
                    i["userList[".concat(a + e, "]")] = t;
                }), n.setData(i);
            }
            n.offset += LIMIT, n.noMore = e.length < LIMIT;
        });
    },
    onOpen: function() {
        var t = this.query, e = t.guid, a = t.issubscribe;
        if (e) return this.overPay();
        if ("false" === a) return _cardUtil.default.saveRadarIcon(this);
        var i = this.data.isOpen;
        this.isChange = !0, this.setData({
            isOpen: !i
        }), wx.showToast({
            title: "".concat(i ? "关闭" : "开启", "成功"),
            mask: !0
        });
    },
    onAuthCancel: function() {
        this.$route("/pages/view/view?type=radarGuide");
    },
    handleOpen: function() {
        var t = this, e = this.data.isOpen;
        api.minicard.card.togglereceive({
            data: {
                action: e ? "open" : "close"
            }
        }).then(function() {
            t.$emit("change.radar.push", e);
        });
    },
    overPay: function() {
        var e = this;
        wx.showModal({
            content: "微信实时接收提醒为高级版企业专属，升级后即可解锁使用",
            confirmText: "去升级",
            success: function(t) {
                t.confirm && (0, _contactUtil.gotoVersion)(e.query.guid, "", !0);
            }
        });
    },
    server: function() {
        (0, _radarUtil.radarService)();
    },
    onAddUser: function() {
        var e = this, t = this.data, a = t.payNum, i = t.userList, r = t.card;
        if (a <= i.length) return wx.showModal({
            content: "当前高级版仅支持".concat(a, "个成员，升级后可继续添加"),
            confirmText: "去升级",
            success: function(t) {
                t.confirm && (0, _contactUtil.gotoVersion)(e.query.guid, "", !0);
            }
        });
        this.$route("/pages/contacts/userselect/userselect?action=radaradd&guid=".concat(r.guid, "&maxSelect=").concat(a - i.length));
    },
    onRemoveUser: function() {
        var t = this.data, e = t.userList, a = t.card;
        e.length && (this.$put("radar.user.list", _toConsumableArray(e)), this.$route("./user-remove/user-remove?guid=".concat(a.guid)));
    }
});